package com.hyzh.latte.util;

import cn.hutool.core.map.MapUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: songkui
 * @date: 2022/7/1 10:46
 */
public class MapKits extends MapUtil {

    public static int mapSize(int size){
        return  (int) (size / 0.75f) + 1;
    }


    public static Map<String, Object> beanToMap(Object object) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            map.put(field.getName(), field.get(object));
        }
        return map;
    }

    /**
     * map转对象
     * @param map
     * @param beanClass
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) throws InstantiationException, IllegalAccessException {
        T object = beanClass.newInstance();
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                continue;
            }
            field.setAccessible(true);
            if (map.containsKey(field.getName())) {
                field.set(object, map.get(field.getName()));
            }
        }
        return object;
    }

/*

    public static void main(String[] args) {

        Long x = new Long(128);
        Long y = 128L;
        Long yi = 128L;
        long z = 128L;

        Integer ii = new Integer(128);
        Integer iii = new Integer(128);
        Integer i2 = 128;
        Integer i4 = 128;
        int i3 = 128;



        //doT(x);
        System.out.println(x.intValue() );
        System.out.println(x == y);
        System.out.println(x == yi);
        System.out.println(y == yi);
        System.out.println(z == yi);
        System.out.println(z == x);
        System.out.println(z == y);
        System.out.println(x.equals(y) );
        System.out.println(y.equals(z) );
        System.out.println("-----------------------");
        System.out.println(ii == iii);
        System.out.println(ii == i2);
        System.out.println(i4 == ii);
        System.out.println(i4 == i2);
        System.out.println(ii == i3);
        System.out.println(i2 == i3);
        System.out.println(ii.equals(i2) );
        System.out.println(i2.equals(i3) );
    }

    private static void doT(Long integer){
        integer = integer.longValue() + 10;
    }
*/


}
